/**
 * 用户相关
 */
import router from '../../router'
import Router from 'vue-router'
import {
  getBaseInfo as ServiceGetUserInfo,
  getUserPermissionSum as ServiceGetUserPermissionSum
} from '@/services/user'

/**
 * 重置路由
 */
function resetRouter () {
  const newRouter = new Router({
    routes: [],
    scrollBehavior: () => ({ y: 0 })
  })
  router.matcher = newRouter.matcher
}

export default {
  namespaced: true, // 启用命名空间，使用的时候是 命名空间/mutation 命名空间/action
  state: {
    id: undefined,
    name: undefined,
    userName: undefined,
    nickName: undefined,
    lastLoginTime: undefined,
    lastLoginIp: undefined,
    avatar: undefined,
    roles: [],
    permission: 0
  },
  getters: {
  },
  /**
   * store 中的状态的唯一方法
   * 通过store.commit()方法触发，来改变状态值
   */
  mutations: {
    'UPDATE_USER_INFO' (state, user) {
      state.id = user.id
      state.name = user.name
      state.userName = user.userName
      state.nickName = user.nickName
      state.lastLoginTime = user.lastLoginTime
      state.lastLoginIp = user.lastLoginIp
      state.avatar = user.avatar
      state.roles.splice(0, state.roles.length, ...user.roles)
    },
    'RESET_USER_INFO' (state) {
      state.id = undefined
      state.name = undefined
      state.userName = undefined
      state.nickName = undefined
      state.lastLoginTime = undefined
      state.lastLoginIp = undefined
      state.avatar = undefined
      state.roles.splice(0, state.roles.length)
    },
    'UPDATE_USER_PERMISSION' (state, value) {
      state.permission = value
    }
  },
  /**
   * 通过store.dispatch()方法来触发
   */
  actions: {
    async 'GetUserInfo' ({ commit, dispatch, state }) {
      let userInfo = null
      try {
        const result = await ServiceGetUserInfo()
        if (result.isSuccess) {
          userInfo = result.data
          // 更新用户信息【不包含权限信息， 权限信息在更新完菜单后更新】
          commit('UPDATE_USER_INFO', userInfo)
          // 获取用户权限
          await dispatch('UpdateUserPermission')
        }
      } catch (error) {
        console.log(error)
      }

      return userInfo
      // 调用接口获取用户信息
      // return ServiceGetUserInfo().then(result => {
      //   if (result.isSuccess) {
      //     const data = result.data
      //     // 更新用户信息【不包含权限信息， 权限信息在更新完菜单后更新】
      //     commit('UPDATE_USER_INFO', data)
      //     // 获取用户权限
      //     dispatch('UpdateUserPermission')
      //   } else {
      //     // 获取用户信息失败
      //     dispatch('UserLogout')
      //   }
      //   return result
      // })
    },
    // 更新用户权限
    'UpdateUserPermission' ({ commit, dispatch, state }) {
      ServiceGetUserPermissionSum().then(result => {
        if (result.isSuccess) {
          if (state.permission !== result.data) {
            const data = result.data
            commit('UPDATE_USER_PERMISSION', data)
            resetRouter()
            dispatch('permission/GenerateRoutes', null, { root: true }).then(routes => {
              router.addRoutes(routes)
            })
          }
        }
      })
    },
    // 用户登录
    'UserLogin' ({ commit, dispatch }, data) {
      commit('RESET_USER_INFO')
      return dispatch('GetUserInfo')
    },
    'UserLogout' ({ commit, dispatch }) {
      commit('RESET_USER_INFO')
    }
  }
}
